<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

require_once('frontend.php');

class Tangiakhang extends Frontend {

    function Tangiakhang() {
        parent::__construct();
    }

    public function index($category = null, $step = null, $action = null) {
        $this->load->model('msamecontent');

        $this->data['category'] = $category;
        $this->data['fix_content_newproject'] = 'fix-content-newproject';
        switch ($category) {
            case 'lien-he':
                $this->contact($category, $step, $action);
                $name_category = 'FORM LIÊN HỆ';
                break;
            case 'gioi-thieu':
                $this->gioithieu($category, $step, $action);
                $name_category = 'Giới thiệu';
                break;
            default :
                $this->data['fix_content_newproject'] = '';
                $this->layout[AppContants::LAYOUT_STEPVIEW] = 'news-construct-home';
                $this->layout[AppContants::LAYOUT_SLIDE_SHOW] = 'slide-show';
                $this->layout[AppContants::LAYOUT_NEW_PROJECT] = 'new-project';
                $this->layout[AppContants::LAYOUT_CUSTOMER] = 'customer';

                $table_db = 'tin_tuc';
                $info_news = $this->msamecontent->getItemEachTable($table_db, 0, 6, AppContants::ACTIVE_STATUS);
                $this->data['info_news'] = $info_news;
                break;
        }
        if (!empty($name_category)) {
            $this->data['name_category'] = $name_category;
        }
    }

    public function loadSameContent($category = null, $type = null) {

        $this->load->library(array('pagination'));
        $this->load->model('msamecontent');
        $this->data['category'] = $category;

        switch ($category) {
            case 'du-an':
                $name_category = 'DỰ ÁN';
                break;
            case 'nha-mau':
                $name_category = 'NHÀ-MẪU';
                break;
            case 'noi-ngoai-that':
                $name_category = 'NỘI NGOẠI THẤT';
                break;
            case 'nha-dat':
                $name_category = 'NHÀ ĐẤT';
                break;
            case 'tin-tuc':
                $name_category = 'TIN TỨC';
                break;
            default :
                $name_category = '';
                break;
        }

        $this->data['fix_content_newproject'] = 'fix-content-newproject';
        $this->data['name_category'] = $name_category;

        $this->layout[AppContants::LAYOUT_STEPVIEW] = 'same-content-project';

        $table_db = str_replace('-', '_', $category);
        $sub_category = '';
        $config['per_page'] = '5';
        $config['num_links'] = 5;
        if ($type == null) {
            $start = 0;
        }

        if (is_numeric($type) || $type == null) {
            if ($this->uri->segment(2) != '') {
                $start = $type;
            }
            $config['uri_segment'] = 2;
            $total_record = $this->msamecontent->getAllItemOfCategory($table_db, AppContants::ACTIVE_STATUS);
            $config['total_rows'] = count($total_record);

            $data = $this->msamecontent->getAllItemOfCategoryLimit($table_db, $start, $config['per_page'], AppContants::ACTIVE_STATUS);
        } else {
            if ($this->uri->segment(3) == '') {
                $start = 0;
            } else {
                $start = $this->uri->segment(3);
            }
            if (!is_numeric($start)) {
                redirect();
            }
            $config['uri_segment'] = 3;
            $sub_category = '/' . $type;
            $total_record = $this->msamecontent->getAllItemByType($table_db, $type, AppContants::ACTIVE_STATUS);
            $config['total_rows'] = count($total_record);
            $data = $this->msamecontent->getAllItemByTypeLimit($table_db, $type, $start, $config['per_page'], AppContants::ACTIVE_STATUS);
        }
        $config['base_url'] = base_url() . $category . $sub_category;
        $this->pagination->initialize($config);
        if ($data == null) {
            redirect();
        }

        $this->data['info'] = $data;
    }

    public function infoItem($category = null, $type = null, $id = null) {
        $this->load->model('msamecontent');
        $this->data['category'] = $category;
        $this->data['fix_content_newproject'] = 'fix-content-newproject';
        $this->layout[AppContants::LAYOUT_STEPVIEW] = 'detail-same-content-project';

        $table_db = str_replace('-', '_', $category);
        $info_item = $this->msamecontent->infoItemById($table_db, $id);

        $item_others = $this->msamecontent->infoItemOtherSameItemCurrent($table_db, $id, 0, 5, AppContants::ACTIVE_STATUS);

        $this->data['info_item'] = $info_item;
        $this->data['item_others'] = $item_others;
    }
     public function gioithieu($category, $step, $action) {
         $this->load->model('mtangiakhang');
         $content = $this->mtangiakhang->getcontentintro();
         $this->data['content']  = $content;
         $this->layout[AppContants::LAYOUT_STEPVIEW] = 'introduction';
     }
    public function contact($category, $step, $action) {
        $this->load->model('mtangiakhang');
        if ($this->session->userdata('send_info_contact') != FALSE) {
            $this->data['send_info_contact'] = $this->session->userdata('send_info_contact');
            $this->session->unset_userdata('send_info_contact');
        }
        if ($action == 'submit' && $this->session->userdata('word') == $this->input->post('security_code')) {
            $data = array(
                'name ' => $this->input->post('ho_ten'),
                'address ' => $this->input->post('dia_chi'),
                'phone ' => $this->input->post('dien_thoai'),
                'email ' => $this->input->post('email'),
                'title ' => $this->input->post('title'),
                'content ' => $this->input->post('no_dung'),
                'created_date ' => date("Y-m-d H:i:s"),
                'status ' => AppContants::ACTIVE_STATUS,
            );
            $this->mtangiakhang->addContact($data);
            $this->session->unset_userdata('word');
            $this->session->set_userdata('send_info_contact', 'success');
            redirect('lien-he');
        }

        $this->getCaptcha();
        $this->layout[AppContants::LAYOUT_STEPVIEW] = 'contact';
    }

    public function getCaptcha() {
        $this->load->library('user');
        $cap = $this->user->captcha();
        $this->data['image'] = $cap['image'];
        $this->session->set_userdata('word', $cap['word']);
    }

}

